<template>
  <div id="home">
    <nav-bar class="home-nav"> <div slot="center">遇见</div> </nav-bar>
    <tab-control
      ref="tabControlFixed"
      :titles="['流行', '新款', '精选']"
      @tabClick="tabClick"
      class="tab-control-fixed"
      v-show="isTabFixed"
    />
    <scroll
      class="content"
      ref="scroll"
      :probeType="3"
      :pullUpLoad="true"
      @scroll="contentScroll"
      @pullingUp="loadMore"
    >
      <home-swiper :banners="banners" @swiperImgLoad="swiperImgLoad" />
      <home-recommend-view :recommends="recommends" />
      <home-feature-view />
      <tab-control
        ref="tabControl"
        :titles="['流行', '新款', '精选']"
        @tabClick="tabClick"
        class="tab-control"
      />
      <goods-list :goods="showGoods" />
    </scroll>
    <back-top @click.native="backClick" v-show="isShowBackTop" />
  </div>
</template>

<script>
import HomeSwiper from './childComps/HomeSwiper.vue'
import HomeRecommendView from './childComps/HomeRecommendView.vue'
import HomeFeatureView from './childComps/HomeFeatureView.vue'

import NavBar from 'components/common/navbal/NavBar'
import Scroll from '../../components/common/scroll/Scroll.vue'

import TabControl from '../../components/content/tabControl/TabControl.vue'
import GoodsList from '../../components/content/goods/GoodsList.vue'
import BackTop from '../../components/content/backTop/backTop.vue'

import { getHomeMultidata, getHomeGoods } from 'network/home'
import { debounce } from 'common/utils'

export default {
  name: 'Home',
  components: {
    HomeSwiper,
    HomeRecommendView,
    HomeFeatureView,
    NavBar,
    Scroll,
    TabControl,
    GoodsList,
    BackTop
  },
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        pop: {
          page: 0,
          list: []
        },
        new: {
          page: 0,
          list: []
        },
        sell: {
          page: 0,
          list: []
        }
      },
      currentType: 'pop',
      isShowBackTop: false,
      tabOffSetTop: 0,
      isTabFixed: false,
      saveY: 0,
      saveYList: {
        pop: 0,
        new: 0,
        sell: 0
      }
    }
  },
  activated() {
    this.$refs.scroll.scrollTo(0, this.saveY, 0)
    this.$refs.scroll.refresh()
  },
  deactivated() {
    this.saveY = this.$refs.scroll.getScrollY()
  },
  created() {
    // * 请求主页需求的多个数据
    this.getHomeMultidata()
    // * 请求主页商品数据
    this.getHomeGoods('pop')
    this.getHomeGoods('new')
    this.getHomeGoods('sell')
  },
  mounted() {
    // * 监听 item 中图片加载完成
    const refresh = debounce(this.$refs.scroll.refresh)
    this.$hub.$on('itemImageLoad', () => refresh())
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list
    }
  },
  methods: {
    // todo 事件监听相关的方法
    tabClick(index) {
      const oldCurrentType = this.currentType

      switch (index) {
        case 0:
          this.currentType = 'pop'
          break
        case 1:
          this.currentType = 'new'
          break
        case 2:
          this.currentType = 'sell'
          break
      }
      // *选项卡内部切换 缓存位置
      this.tabCut(oldCurrentType)

      this.$refs.tabControlFixed.currentIndex = index
      this.$refs.tabControl.currentIndex = index
    },
    tabCut(oldCurrentType) {
      const newCurrentType = this.currentType
      const scto = this.$refs.scroll.getScrollY()
      const type = this.saveYList[this.currentType]
      // 判断是否吸顶
      if (this.isTabFixed) {
        // 判断即将进入的选项卡 是否 没 储存位置参数
        if (this.saveYList[newCurrentType] === 0) {
          // 没 则 滚动至选项卡吸顶位置
          this.$refs.scroll.scrollTo(0, -this.tabOffSetTop, 20)
        } else {
          // 有 则 直接滚动至上次离开时的位置
          this.$refs.scroll.scrollTo(0, type, 20)
        }
        // 把当前离开时的位置信息存入 saveYList 对象中， 存入即将离开的那个属性
        this.saveYList[oldCurrentType] = scto
        this.$refs.scroll.refresh()
      }
    },
    backClick() {
      this.$refs.scroll.scrollTo(0, 0)
    },
    contentScroll(position) {
      // 1、判断 backtop 是否显示
      this.isShowBackTop = -position.y > 1000
      // 2、决定 tabcontrol 是否吸顶
      this.isTabFixed = -position.y > this.tabOffSetTop
    },
    // 轮播图加载完成后 获取选项卡标签需要滚动至悬浮的距离
    swiperImgLoad() {
      this.tabOffSetTop =
        this.$refs.tabControl.$el.offsetTop -
        this.$refs.tabControl.$el.offsetHeight
    },
    // * 下拉加载更多
    loadMore() {
      this.getHomeGoods(this.currentType)
    },
    // ? 网络请求相关的方法
    async getHomeMultidata() {
      const { data: res } = await getHomeMultidata()
      this.banners = res.banner.list
      this.recommends = res.recommend.list
    },
    async getHomeGoods(type) {
      const page = this.goods[type].page + 1
      const { list } = await getHomeGoods(type, page)
      this.goods[type].list.push(...list)
      this.goods[type].page += 1
      this.$refs.scroll.finishPullUp()
    }
  }
}
</script>

<style scoped>
#home {
  height: 100vh;
  overflow: hidden;
}
.home-nav {
  /* z-index: 1;
  position: fixed;
  left: 0;
  top: 0;
  right: 0; */
  color: #ffffff;
  background-color: var(--color-tint);
}
.tab-control {
  position: static;
}
.tab-control-fixed {
  position: fixed;
  left: 0;
  right: 0;
}
.content {
  height: calc(100% - 93px);
  overflow: hidden;
}
</style>
